﻿Imports GYCrack.My.MyApplication
Public Class frmAbout

    Const AboutPage As String = "http://gy-crack-suite.googlecode.com/svn/wiki/Webpage/About.html"

    Private Sub frmAbout_Activated(sender As Object, e As System.EventArgs) Handles Me.Activated
        If frmBrowser.Visible Then frmBrowser.Activate()
    End Sub

    Private Sub frmAbout_LocationChanged(sender As Object, e As System.EventArgs) Handles Me.LocationChanged
        If frmBrowser.Visible Then frmBrowser.Location = Me.PointToScreen(New Point(0, 0))
    End Sub

    Private Sub frmAbout_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        frmMain.Show()
    End Sub

    Private Sub frmAbout_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Terminate = True
        Player.Dispose()
    End Sub

    Private Sub frmAbout_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ApplicationTitle As String
        If My.Application.Info.Title <> "" Then
            ApplicationTitle = My.Application.Info.Title
        Else
            ApplicationTitle = System.IO.Path.GetFileNameWithoutExtension(My.Application.Info.AssemblyName)
        End If
        Me.Text = ApplicationTitle
        Me.LabelProductName.Text = My.Application.Info.ProductName
        Me.LabelVersion.Text = My.Application.Info.Version.ToString()
        Me.LabelCompanyName.Text = My.Application.Info.CompanyName
        Me.LabelCopyright.Text = My.Application.Info.Copyright
        Me.txtAutorization.Text = "已授权给：" & CStr(RedeemCode)
        MotionRun()
    End Sub

    Dim TextFull(4) As String
    Dim SizeFull(4) As Size
    Delegate Sub TaskDelegate(id As Integer, arg As Integer)
    Dim MotionThread As New TaskDelegate(AddressOf Motion)
    Friend Player As TG.Sound.OggPlay
    Dim Terminate As Boolean
    Dim MouseFunctionEnabled As Boolean

    Private Sub MotionRun()
        TextFull(0) = LabelProductName.Text
        TextFull(1) = LabelVersion.Text
        TextFull(2) = LabelCompanyName.Text
        TextFull(3) = LabelCopyright.Text
        TextFull(4) = txtAutorization.Text
        SizeFull(0) = LabelProductName.Size
        SizeFull(1) = LabelVersion.Size
        SizeFull(2) = LabelCompanyName.Size
        SizeFull(3) = LabelCopyright.Size
        SizeFull(4) = txtAutorization.Size
        LabelProductName.Hide()
        LabelVersion.Hide()
        LabelCompanyName.Hide()
        LabelCopyright.Hide()
        txtAutorization.Hide()
        btnReAuth.Hide()
        btnMore.Hide()
        Terminate = False
        MouseFunctionEnabled = False
        MotionThread.BeginInvoke(0, 0, Nothing, Nothing)
    End Sub

    Private Sub Motion(arg0 As Integer, arg1 As Integer)
        Dim id As Integer = 0
        Do
            For i = 0 To 12 + TextFull(id).Length
                MotionCallback(id, i)
                Threading.Thread.Sleep(100)
                If Terminate Then Return
            Next
            id += 1
        Loop While id <= 4
        MotionCallback(-1, 0)
        Return
    End Sub

    Private Sub MotionCallback(id As Integer, arg As Integer)
        If Terminate Then Return
        On Error Resume Next
        If Me.LabelProductName.InvokeRequired Then
            Dim b As New TaskDelegate(AddressOf MotionCallback)
            If Terminate Then Return
            Me.Invoke(b, New Object() {id, arg})
        Else
            If id = -1 Then
                MouseFunctionEnabled = True
                btnReAuth.Show()
                btnMore.Show()
            Else
                Dim Target As Label
                If id = 0 Then
                    Target = LabelProductName
                ElseIf id = 1 Then
                    Target = LabelVersion
                ElseIf id = 2 Then
                    Target = LabelCompanyName
                ElseIf id = 3 Then
                    Target = LabelCopyright
                Else
                    Target = txtAutorization
                End If
                If arg = 0 Then
                    Target.AutoSize = False
                    Target.Size = New Size(0, 0)
                    Target.Text = ""
                    Target.Show()
                ElseIf arg <= 8 Then
                    Target.Size = New Size(CInt(SizeFull(id).Width / 8 * arg), CInt(SizeFull(id).Height / 8 * arg))
                ElseIf (arg = 9) OrElse (arg = 11) Then
                    Target.BackColor = Color.LightSkyBlue
                ElseIf (arg = 10) OrElse (arg = 12) Then
                    Target.BackColor = Color.DodgerBlue
                Else
                    Target.Text = Mid(TextFull(id), 1, arg - 12)
                End If
            End If
        End If
    End Sub

    Private Sub btnMute_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles btnMusic.CheckedChanged
        If btnMusic.Checked Then
            Player = New TG.Sound.OggPlay(Me, TG.Sound.OggSampleSize.SixteenBits)
            Player.PlayOggFile(My.Resources.Music, 1)
        Else
            Player.Dispose()
        End If
    End Sub

    Private Sub Labels_MouseEnter(sender As Object, e As System.EventArgs) Handles LabelProductName.MouseEnter, LabelVersion.MouseEnter, LabelCopyright.MouseEnter, LabelCompanyName.MouseEnter, txtAutorization.MouseEnter
        If MouseFunctionEnabled Then CType(sender, Label).BackColor = Color.LightSkyBlue
    End Sub

    Private Sub Labels_MouseLeave(sender As Object, e As System.EventArgs) Handles LabelProductName.MouseLeave, LabelVersion.MouseLeave, LabelCopyright.MouseLeave, LabelCompanyName.MouseLeave, txtAutorization.MouseLeave
        If MouseFunctionEnabled Then CType(sender, Label).BackColor = Color.DodgerBlue
    End Sub

    Delegate Sub SizeSwitch(Arg As Object)
    Dim NormalHeight As Integer
    Public Shrink As New SizeSwitch(AddressOf ShrinkHandler)
    Public Extend As New SizeSwitch(AddressOf ExtendHandler)

    Private Sub ShrinkHandler(Arg As Object)
        On Error Resume Next
        Terminate = False
        Dim Height As Integer = Me.Height
        Do Until Height <= NormalHeight
            Height -= 3
            If Not Terminate Then Me.Invoke(New SizeSwitch(AddressOf OnSizeSwitch), New Object() {Height})
            Threading.Thread.Sleep(10)
        Loop
        If Not Terminate Then Me.Invoke(New SizeSwitch(AddressOf OnSizeSwitch), New Object() {NormalHeight})
    End Sub

    Private Sub ExtendHandler(Arg As Object)
        On Error Resume Next
        Terminate = False
        NormalHeight = Me.Height
        Dim Height As Integer = Me.Height
        Do Until Height >= Me.Width
            Height += 3
            If Not Terminate Then Me.Invoke(New SizeSwitch(AddressOf OnSizeSwitch), New Object() {Height})
            Threading.Thread.Sleep(10)
        Loop
        If Not Terminate Then Me.Invoke(New SizeSwitch(AddressOf OnSizeSwitch), New Object() {Me.Width})
    End Sub

    Private Sub OnSizeSwitch(Height As Object)
        Me.Height = CInt(Height)
        Me.Refresh()
    End Sub

    Private Sub btnMore_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles btnMore.LinkClicked
        Extend.BeginInvoke(Nothing, Nothing, Nothing)
        frmBrowser.Init(AboutPage, Me)
        frmBrowser.Location = Me.PointToScreen(New Point(0, 0))
    End Sub

    Private Sub btnReAuth_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles btnReAuth.LinkClicked
        Dim RegFilePath As String = My.Application.Info.DirectoryPath & "\GYCrack.key"
        If MsgBox("您当前的邀请码为" & CStr(RedeemCode) & "，该邀请码可继续使用至" & GetValidDate() & "。您确定要提前更换吗？" & vbCrLf & "（更换前请您牢记您当前的邀请码，以便您在更换失败时可以重新激活）", MsgBoxStyle.OkCancel Or MsgBoxStyle.Exclamation, "更换邀请码") = MsgBoxResult.Cancel Then Exit Sub
        My.Computer.FileSystem.DeleteFile(RegFilePath)
        Application.Restart()
    End Sub

    Private Function GetValidDate() As String
        For i = 0 To 29
            If GetRedeemCode(Today - New TimeSpan(i, 0, 0, 0)) = RedeemCode Then
                Return (Today + New TimeSpan(30 - i, 0, 0, 0)).ToString("yyyy年MM月dd日")
            End If
        Next
        Return New Date(0).ToString("yyyy年MM月dd日")
    End Function

End Class
